<%@ page contentType="text/html; charset=UTF-8"%>
<div style="width: 1000px; margin: 0 auto;display: none;" id="upgradeDiv">
<table id="upgradeFileList" border="1" style="border-collapse: collapse; width: 100%; border: 1px solid #e4e4e4;margin-top: 30px">
	
	

</table>
<table border="0" style="border-collapse: collapse; width: 100%; border: 1px solid #e4e4e4;margin-top: 00px">
			
			
			
				<tr>
					<td class="d">上传升级文件：</td>
					<td class="d">
						<label>文件：</label>
						<input id="upgradeFile"  type="file" size="400"   style="width: 300px;"   name="upgradeFile"  class="input">
					</td>
					<td class="d"><input type="button" onclick="uploadeUpgradeFileUp()" value="上传"/></td>
				</tr>
		
</table>



<table border="1" style="border-collapse: collapse; width: 100%; border: 1px solid #e4e4e4;margin-top: 30px;">
			
			<tr><th class="d" colspan="2" >设备升级</th></tr>
				<tr>
					<td class="d" style="text-align: left;">
					
					
						<div style="float: left;margin-top: 5px;" id="upgradeFiles">
						</div>
						
						<div style="float: right;">
							 <input type="button"  style="height: 30px;width: 100px;float: right;"  value="升级" onclick="upgradeDeviceAction()" />
						</div>
						
					
							
					</td>
					
				</tr>
</table>




<div style="border-collapse: collapse; width: 100%; border: 1px solid #e4e4e4;text-align: left;">

		
	
		<table id="upgeadeDeviceList" style="width: 100%;margin-top: 0px;border: 1px solid #e4e4e4;border-collapse: collapse; " border="1">
	
	 
		 	
		 
		</table>
	
	</div>

	<div style="width: 100%;height: 30px;"></div>
</div>
<script>

	getUpgradeFileList();

 function getUpgradeFileList(){
	 
	 

		$.post("upgradeAction", {
			"actionType" : "getUpgradeFileList"
		}, function(data) {
		
			updateUpgradeFileList(data);
			
		},"json");
	 
	 
 }
 
 
 function updateUpgradeFileList(data){
	 
	 var html = "<tr><th colspan='4' class='d'>升级文件</th> </tr>";
	 var heml2 ="升级文件:<select id='upgradeFileSelect'>";
	 if(data.length>0){
		 for(var i = 0; i <data.length;i++){
				var fileBean =  data[i];
				 html += "<tr>";
					 html += "<td class='d'>"+fileBean.fileName+"</td>";
					 html += "<td class='d'>上传日期："+fileBean.upTime+"</td>";
					 
					 if(fileBean.size>=1){
						 html += "<td class='d'>大小:"+fileBean.size+"KB</td>";
					 }else{
						 
						 html += "<td class='d'>大小:<1KB</td>";
					 }
					
					 html += "<td class='d'><input type='button' onclick='delUpgradeFile(\""+fileBean.fileName+"\")' value='删除'/></td>";
				html += " </tr>	";
				 
				
					heml2+="<option value='"+fileBean.fileName+"'>"+fileBean.fileName+"</option>";
				
			 }
		 
		 
		 
		 
		 
	 }else{
		 
		 
		 heml2+="<option>没有升级文件</option>";
		 html += "<tr><td colspan='4' class='d'>没有升级文件</td> </tr>";
	 }
	 
	 
	 heml2+="</select>";
	 $("#upgradeFiles").html(heml2);
	
	$("#upgradeFileList").html(html);
	 
 }

 
 function delUpgradeFile(delUpgradeFileName){
	 
	 
		$.post("upgradeAction", {
			"actionType" : "delUpgradeFile","fileName":delUpgradeFileName
		}, function(data) {
			getUpgradeFileList();
			alert(data.msg);
		},"json");
	 
 }
 
 function upgradeDeviceAction(){
	 
	 if(upgradeDeviceArray.length<1){
		 
		 alert("请选择升级设备");
		 return ;
	 }

		$.post("upgradeAction", {
			"actionType" : "upgradeDevice","fileName":$("#upgradeFileSelect").val(),"devices":upgradeDeviceArray.toString()
		}, function(data) {
		
			alert(data.msg);
			
		},"json");
	 
 }
 
 
 function uploadeUpgradeFileUp(){
	 
	 if ($("#upgradeFile").val().length > 0) {
         ajaxFileUpload();
     }
     else {
         alert("请选择文件");
     }
 }
 
 function ajaxFileUpload() {
     $.ajaxFileUpload({  
         url : "./UploadUpgradeFileAction",   //submit to UploadFileServlet  
         secureuri : false,  
         fileElementId : 'upgradeFile',  
         dataType : 'text', 
         success : function(data, status) {  
        	 
        	 getUpgradeFileList();
        	 alert(data);
        
         },  
         error : function(data, status, e) {  
        	   alert(e);
         }  
     }); 
     
 }
 

 //加载界面的时候默认加载设备
 getDevices();
 
 
 //获取设备列表
 function getDevices(){
		$.post("upgradeAction", {
			"actionType" : "getDevices"
		}, function(data) {
		
			
			updateDeviceTable(data);
			
		},"json");
 }
 //更新设备列表
function updateDeviceTable(data){

	 
	 
	var html = "";
	 
	 if(data!=null){
		 
		 
			for(var key in data){
				
				
				var mapNet = data[key];
				 html+="<tr><th class='d' colspan='3' style='text-align: left;'><label>地图"+key+"</label>";
				 html+="<div style='width: 120px;float: right; margin-right: 140px;display:none;' >全选<input   type='checkbox'></div></th></tr>";
					 for(var netId in mapNet){
						 
						var baseStations  =  mapNet[netId];
						
						 for(var baseStationsId in baseStations){
							 
							 
							 html+= "<tr>";
							 
							 if(baseStations[baseStationsId]==0){
								 html+="<td class='d' style='text-align: left;'>基站"+baseStationsId+"</td>";
							 }
							 html+="<td class='d'>基站网络"+netId+" </td>";
							 html+="<td class='d'><input id='"+key+"_"+netId+"_"+baseStationsId+"' onclick='selectUpgeadeDevice(\""+key+"_"+netId+"_"+baseStationsId+"\")' type='checkbox' /></td>";
							 html+=" </tr>";
						 }
						 
					 }
			}
	 }
	 if(html.length==0){
		 html+="<tr><th class='d' colspan='3' style='text-align: left;'><label>没有设备</label>";
		 
	 }
	 $("#upgeadeDeviceList").html(html);
	 
 }

 var upgradeDeviceArray = new Array();
 
 function selectUpgeadeDevice(data){
	 
	 if($("#"+data).attr("checked")){
		 
		 upgradeDeviceArray[upgradeDeviceArray.length] = data;
	 }else{
		 
		 upgradeDeviceArray =  delUpgradeDeviceArray(data);
	
	 }
	 
		

 }
 
 //删除已勾选中的不升级的基站
 function delUpgradeDeviceArray(value){
	 
	 var newUpgradeDeviceArray = new Array();
	 
	 var j =0;
	 for(var i = 0 ; i < upgradeDeviceArray.length;i++){
		 
		 if(upgradeDeviceArray[i] != value){
			 
			 newUpgradeDeviceArray[j] = upgradeDeviceArray[i];
			 j++;
		 }
	 }
	 
	return  newUpgradeDeviceArray;
 }

 

</script>